﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;

namespace VitCode.Castle.Management
{
    //AMS试用版的管理限制类
   public static class DemoVersionManager
    {
        public static   bool CheckIsDemoVersion(string className, StringCollection scObjErrMsg)
        {
            var acc = new BaseDataAccess();
            if (className == "AssetInfo")
            {
                IList list = acc.Session.CreateQuery("From AssetInfo").List();
                if (list != null && list.Count >= 60)
                {
                    scObjErrMsg.Add("AMS试用版最多只能保存60条资产信息！");
                    return true;
                }
            }
            if(className == "User")
            {
                IList existUser = acc.Session.CreateQuery("From User").List();
                if (existUser != null && existUser.Count > 0)
                {
                    int existUserCount = existUser.Count;
                    if (existUserCount >= 3)
                    {
                        scObjErrMsg.Add("AMS试用版最多只能有3个系统用户!");
                        return true;  
                    }
                      
                }
            }
            return false;
        }

        public static bool CheckIsDemoVersionOfImport(string filePath, string sheetName)
        {
            if (CheckIsAssetImport(filePath))
            {
                var helper = new ExcelHelper(filePath, sheetName);
                var acc = new BaseDataAccess();
                var dt = helper.GetDataTable();
                int importCount = 0;
                int existCount = 0;
                if (dt != null && dt.Rows.Count > 0)
                    importCount = dt.Rows.Count;
                IList list = acc.Session.CreateQuery("From AssetInfo").List();
                if (list != null && list.Count > 0)
                    existCount = list.Count;
                if (existCount + importCount > 60)
                {
                    return false;
                }
                return true;
            }
            return true;
        }

        private static bool CheckIsAssetImport(string filePath)
        {
            string strFileName = filePath;
            int index = strFileName.LastIndexOf("\\");
            strFileName = strFileName.Substring(index + 1, strFileName.Length - index - 1);
            strFileName = strFileName.ToUpper();
            return (strFileName.IndexOf("主资产导入") >= 0) ||
                   (strFileName.IndexOf("低值易耗品导入") >= 0) ||
                   (strFileName.IndexOf("备品备件导入") >= 0) ||
                   (strFileName.IndexOf("依附性资产导入") >= 0);
        }

        public static bool IsDemo()
       {
           return false;
       }


 
    }
}
